package com.example.service;

import com.example.jiekou.SearchOrder;
import com.example.shiti.ProductItemInfo;
import com.example.shiti.TradeInfo;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SearchService {

    private final SearchOrder searchOrder;

    @Autowired
    public SearchService(SearchOrder searchOrder) {
        this.searchOrder = searchOrder;
    }

    @PostConstruct
    public void init() {
        System.out.println("SearchOrder injected: " + searchOrder);
    }

    public TradeInfo getLatestCreateTime() {
        return searchOrder.findLatestCreateTime();
    }
    public TradeInfo getFirstCreateTime() {
        return searchOrder.findFirstCreateTime();
    }
    public List<TradeInfo> findTradeInfoBetweenCreateTimes(Date startTime, Date endTime, Sort sort, String key, String value,String status){
        return searchOrder.findTradeInfoBetweenCreateTimes(status, startTime, endTime, sort);
    }
    public Map<String,List<ProductItemInfo>> findTradeInfosByTradeInfoIds(List<String> tradeInfoIds){
        return searchOrder.findTradeInfosByTradeInfoIds(tradeInfoIds).stream().collect(Collectors.groupingBy(ProductItemInfo::getTradeInfoId));
    }
    public Long countOrdersByStatusAndCreateTimeRange(Date startTime, Date endTime,String status){
        return searchOrder.countOrdersByStatusAndCreateTimeRange(status,startTime,endTime);
    }
}
